﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Windows.Forms;

namespace Compiler.LabForms
{
    public partial class ConstantEdit : Form
    {
        private ListView _constantsView;
        private ListViewItem _item;

        public ConstantEdit(ListView constantsView)
        {
            _constantsView = constantsView;
            InitializeComponent();
            Text = @"Добавление константы";
        }

        public ConstantEdit(ListView constantsView, ListViewItem item)
        {
            _constantsView = constantsView;
            _item = item;
            InitializeComponent();
            Text = @"Редактирование константы";
            valueBox.Text = _item.SubItems[1].Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(valueBox.Text)) return;
            if (_item != null)
            {
                _item.SubItems[1].Text = valueBox.Text;
            }
            else
            {
                var newItem = new ListViewItem
                {
                    Text = Lab.ConstantId.ToString(CultureInfo.InvariantCulture),
                };
                newItem.SubItems.Add(valueBox.Text);
                _constantsView.Items.Add(newItem);
                Lab.ConstantId++;
            }
            _constantsView.SelectedItems.Clear();
            Close();
        }
    }
}
